<!-- 
 * 需求管理
 * @author: 冯宇
 * @since: 2022-05-16
 * index.vue
-->
  <template>
  <div class="patent-container h100">
    <router-view v-show="routerCutLevel === 3" class="detailsPage"></router-view>
    <div v-show="routerCutLevel === 2" class="listPage">
      <Filters :formModel="data.formModel" :formLabel="data.formLabel" @reset="resizeForm" @filter="getPatentList">
      </Filters>

      <TableCom :tableHeader="data.tableHeader" :tableData="data.tableData" :btnData="data.btnData"
        @getCurrentChange="getCurrentChange" @getSizeChange="getSizeChange" @comEvent="comEvent"
        @getSelectChange="getSelectChange" :isShowCheckbox="true" @colSelect="colSelect" @refresh="refreshTable"
        :pagination="data.pagination" :loading="data.loading" handleWidth="252">
        <el-button v-for="(item, index) in data.btnList" :key="index" class="tableBtnCss"
          :style="{ backgroundColor: item.bgColor }" @click="userManageBtnList(index)">
          <svg-icon :name="item.icon" v-if="item.icon"></svg-icon>
          {{ item.name }}
        </el-button>
      </TableCom>

          <!-- 上传文件 -->
    <el-dialog v-model="isShowLead" title="专利导入" width="30%" :before-close="() => (isShowLead = false)">
      <uploadDownload @upload="uploadFile"></uploadDownload>
    </el-dialog>
    </div>
  </div>
</template>

<script setup>
import { reactive, computed ,ref} from 'vue'
import { getDomainApi } from '@/api/public/publicDict.js'
import { patentListApi, patentBatchApi } from '@/api/interface/patent.js'
import { colFilters, batchHandle, lineHandle } from '@/utils/common.js'

import { ElMessageBox, ElMessage } from 'element-plus'

import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
const router = useRouter()
const store = useStore()
const isShowLead = ref(false)

const routerCutLevel = computed(() => {
  return store.getters['setting/routerCutLevel']
})

const data = reactive({
  formModel: {
    patentTitle: '',
    industryCodeOne: '',
    patentPn: '',
  },
  formLabel: [
    {
      label: '专利名称',
      model: "patentTitle",
      type: "text",
      width: '160',
      placeholder: '请输入专利名称',
    },
    {
      label: '专利号',
      model: "patentPn",
      type: "text",
      width: '160',
      placeholder: '请输入专利号',
    },
    {
      label: '所属领域',
      model: "industryCodeOne",
      type: "select",
      width: '160',
      option: [
        {
          key: '全部',
          value: ''
        },
      ],
    },
  ],
  btnList: [
    {
      name: '新增专利',
      bgColor: '#25D390',
    },
    // {
    //   name: '专利导入',
    //   bgColor: '#00A2FF',
    // },
    {
      name: '批量启用',
      bgColor: '#00A2FF',
    },
    {
      name: '批量禁用',
      bgColor: '#FFB018',
    },
    {
      name: '批量删除',
      bgColor: '#F74B33',
    },
  ],
  tableHeader: [
    {
      label: '专利号',
      prop: 'patentPn',
      isLink: true,
      function: 'detail'
    },
    {
      label: '专利标题',
      prop: 'dynamicTitle',
    },
    {
      label: '所属领域',
      prop: 'industryNameOne',
    },
  ],
  tableData: [],
  btnData: [
    {
      name: '编辑',
      color: '#25D390',
      change: 'edit',
    },
    {
      name: '删除',
      color: '#F74B33',
      change: 'del',
    },
    {
      name: '是否启用',
      isStart: true,
      prop: 'valid',
      change: 'enable',
    },
    {
      name: '是否热门',
      isStart: true,
      prop: 'isHot',
      change: 'isHot',
    },
  ],
  currentSelectList: [],
  pagination: {
    currentPage: 1,
    size: 10,
    total: 0,
  },
  loading:false,
})


//获取专利列表及搜索
const getPatentList = () => {
  data.loading = true
  const params = {
    dynamicTitle: data.formModel.patentTitle,
    industryCodeOne: data.formModel.industryCodeOne,
    patentPn: data.formModel.patentPn,
    ipi: data.pagination.currentPage,
    ips: data.pagination.size,
  }
  patentListApi(params).then((res) => {
    if (res.status === 200) {
      data.tableData = res.data.result
      data.loading = false
      data.pagination.total = res.data.total
    }
  })
}
getPatentList()
//重置搜索条件
const resizeForm = () => {
  data.formModel.patentTitle = ''
  data.formModel.industryCodeOne = ''
  data.formModel.patentPn = ''
  getPatentList()
}




//获取一级领域
const getDomain = () => {
  getDomainApi({ parentId: 0 }).then((res) => {
    if (res.status === 200) {
      let arr = res.data.map(v => ({
        key: v.keyName,
        value: v.keyId
      }))
      for (let v of data.formLabel) {
        if (v.model == 'industryCodeOne') {
          v.option = [...v.option, ...arr]
          break
        }
      }
    }
  })
}
getDomain()






//列筛选
const colSelect = (headerArr, type, e, currentItem) => {
  if (type === 'filter') {
    const currentData = colFilters(headerArr, e, currentItem, data.tableHeader)
    data.tableHeader = currentData
  } else {
    //全选/全取消
    data.tableHeader = headerArr
  }
}
//刷新
const refreshTable = () => {
  getPatentList()
}
//表头功能
const userManageBtnList = (index) => {
  switch (index) {
    case 0:
      console.log('新增')
      router.push({ name: 'AddPatent' })
      break
    case 1:
      isShowLead.value = true
      console.log('通过文件批量导入')
      break
    case 2:
      const startArr = data.currentSelectList.map(v => v.id)
      const startFunction = () => {
        const params = { batchType: 'START', ids: startArr }
        patentBatchApi(JSON.stringify(params)).then(res => {
          if (res.status === 200) {
            ElMessage({
              message: res.message,
              type: 'success',
            })
            getPatentList()
          }
        })
      }
      batchHandle(startArr, 'START', startFunction)
      break
    case 3:
      const stopArr = data.currentSelectList.map(v => v.id)
      const stopFunction = () => {
        const params = { batchType: 'STOP', ids: stopArr }
        patentBatchApi(JSON.stringify(params)).then(res => {
          if (res.status === 200) {
            ElMessage({
              message: res.message,
              type: 'success',
            })
            getPatentList()
          }
        })
      }
      batchHandle(stopArr, 'STOP', stopFunction)
      break
    case 4:
      const delArr = data.currentSelectList.map(v => v.id)
      const delFunction = () => {
        const params = { batchType: 'DEL', ids: delArr }
        patentBatchApi(JSON.stringify(params)).then(res => {
          if (res.status === 200) {
            ElMessage({
              message: res.message,
              type: 'success',
            })
            getPatentList()
          }
        })
      }
      batchHandle(delArr, 'STOP', delFunction)
      break
    default:
      break
  }
}
//勾选需求
const getSelectChange = (row) => {
  data.currentSelectList = row
}
//分页
const getCurrentChange = (page) => {
  data.pagination.currentPage = page
  getPatentList()
}
//每页多少条
const getSizeChange = (size) => {
  data.pagination.size = size
  getPatentList()
}

//上传文件
const uploadFile = (data) => {
  console.log(data, 'data');

}


//行内功能
const comEvent = (type, row) => {
  if (type == 'detail') {
    sessionStorage.patentId = row.patentPn
    router.push({ name: 'PatentDetail', params: { id: row.patentPn } })
  }
  else if (type == 'edit') {
    router.push({ name: 'EditPatent', params: { id: row.id } })
  }
  else if (type == 'del') {
    const delFunction = () => {
      let params = {
        batchType: 'DEL',
        ids: [row.id],
      }
      patentBatchApi(JSON.stringify(params)).then((res) => {
        if (res.status === 200) {
          ElMessage({
            message: res.message,
            type: 'success',
          })
          getPatentList()
        }
      })
    }
    lineHandle(row, 'del', delFunction)
  }
  else if (type == 'enable') {
    const validFunction = () => {
      let params = {
        batchType: row.valid ? 'STOP' : 'START',
        ids: [row.id],
      }
      patentBatchApi(JSON.stringify(params)).then((res) => {
        if (res.status === 200) {
          ElMessage({
            message: res.message,
            type: 'success',
          })
          row.valid = !row.valid
        }
      })
    }
    lineHandle(row, 'valid', validFunction)
  }
  else if (type == 'isHot') {
    const hotFunction = () => {
      let params = {
        batchType: row.isHot ? 'STOP_HOT' : 'START_HOT',
        ids: [row.id],
      }
      patentBatchApi(JSON.stringify(params)).then((res) => {
        if (res.status === 200) {
          ElMessage({
            message: res.message,
            type: 'success',
          })
          row.isHot = !row.isHot
        }
      })
    }
    lineHandle(row, 'isHot', hotFunction)
  }
}
</script>

<style lang="scss" scoped>
</style>